#include "stdafx.h"
#include "config.h"
#include "Thickness.h"

namespace DeepZoomImage
{
	Thickness::Thickness (): left (0), top (0),right (0), bottom (0)
	{

	}

	Thickness::Thickness(double uniform)
	{
		bottom = uniform;
		right = uniform;
		left = uniform;
		top = uniform;
	}

	Thickness::Thickness(double hori, double vert)
	{
		bottom = vert;
		right = hori;
		left = hori;
		top = vert;
	}

	Thickness::Thickness(double left, double top, double right, double bottom)
	{
		this->bottom = bottom;
		this->right = right;
		this->left = left;
		this->top = top;
	}

	Thickness Thickness::operator- ()
	{
		return Thickness (-left, -top, -right, -bottom);
	}

	Thickness Thickness::operator+ (const Thickness &th)
	{
		return Thickness (left + th.left, top + th.top, right + th.right, bottom + th.bottom);
	}

	Thickness Thickness::operator- (const Thickness &th)
	{
		return Thickness (left - th.left, top - th.top, right - th.right, bottom - th.bottom);
	}

	bool Thickness::operator == (const Thickness &v) const
	{
		return fabs (bottom - v.bottom) < DBL_EPSILON && fabs(right - v.right) < DBL_EPSILON && fabs (left - v.left) < DBL_EPSILON && fabs (top - v.top) < DBL_EPSILON;
	}

	bool Thickness::operator != (const Thickness &v) const
	{
		return !(*this == v);
	}

	bool Thickness::FromStr (const char *str, Thickness *thickness)
	{
		GArray *values = double_garray_from_str (str, 0);
		bool rv = true;

		switch (values->len) 
		{
		case 0:
			*thickness = Thickness ();
			break;
		case 1:
			*thickness = Thickness (g_array_index (values, double, 0));
			break;
		case 2:
			*thickness = Thickness (g_array_index (values, double, 0),
				g_array_index (values, double, 1));
			break;
		case 4:
			*thickness = Thickness (g_array_index (values, double, 0),
				g_array_index (values, double, 1),
				g_array_index (values, double, 2),
				g_array_index (values, double, 3));
			break;
		default:
			g_warning ("Thickness specified with %d values, '%s'.", values->len, str);
			rv = false;
			break;
		}

		if (values)
			g_array_free (values, TRUE);
		return rv;
	}
}